/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.com.procesos;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import pe.com.ColaProcesamiento;
import pe.com.Util;
import pe.com.unidades.Multas;
import pe.com.unidades.Papeletas;
import pe.com.unidades.Placa;

/**
 *
 * @author romulogalindo
 */
public class ProcesadorPlaca extends Proceso implements Runnable {

    private List<Placa> placas;
    private int ID;
    DefaultHttpClient client;
    String __VIEWSTATE;
    String __EVENTVALIDATION;
    String rk;
//    FileWriter archivo;

    public ProcesadorPlaca(List<Placa> placas, int ID) {
        this.placas = placas;
        this.ID = ID;

        try {
            client = new DefaultHttpClient();
//            HttpGet http = new HttpGet("http://www.sat.gob.pe");
            HttpGet http = new HttpGet("http://www.sat.gob.pe/Websitev8/home.aspx?i=a");
//            HttpGet http = new HttpGet("https://sat.gob.pe");
            HttpResponse resp = client.execute(http);
            String rpta1 = IOUtils.toString(resp.getEntity().getContent(), "UTF-8");
//            System.out.println("===========================1================================================");
//            System.out.println(rpta1);
//            System.out.println("============================================================================");
            //-------------------------------------------------------
//            SSLSocketFactory sf = new SSLSocketFactory(
//                    SSLContext.getInstance("SSl"),
//                    //                    SSLContext.getInstance("TLS"),
//                    SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
//            Scheme sch = new Scheme("https", 443, sf);
//            client.getConnectionManager().getSchemeRegistry().register(sch);

            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String string, SSLSession ssls) {
                    System.out.println("Interno: string:" + string + ", ssls:" + ssls);
                    return true;
                }
            });

            //-------------------------------------------------------
            HttpPost http2 = new HttpPost("http://www.sat.gob.pe/Websitev8/InicioOV.aspx?t=2&v=D1S615");
            http2.getParams().setParameter("http.protocol.handle-redirects", true);
            client.setRedirectStrategy(new DefaultRedirectStrategy() {
                public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
                    boolean isRedirect = false;
                    try {
                        isRedirect = super.isRedirected(request, response, context);
                    } catch (ProtocolException e) {
                        e.printStackTrace();
                    }
                    if (!isRedirect) {
                        int responseCode = response.getStatusLine().getStatusCode();
                        if (responseCode == 301 || responseCode == 302) {
                            return true;
                        }
                    }
                    return isRedirect;
                }
            });
            org.apache.http.HttpResponse resp2 = client.execute(http2);

            InputStream instream2 = resp2.getEntity().getContent();
            String rpta2 = IOUtils.toString(instream2, "UTF-8");
//            System.out.println("========================2===================================================");
//            System.out.println(rpta2);
//            System.out.println("============================================================================");
            rpta2 = rpta2.substring(rpta2.indexOf("bienvenida.aspx?"), rpta2.length());
            rpta2 = rpta2.substring(0, rpta2.indexOf("FRAMEBORDER") - 2);

            rk = "https://www.sat.gob.pe/homesite2007/modulos/" + rpta2.replace("bienvenida", "papeletas");
            HttpGet http3 = new HttpGet(rk);
            org.apache.http.HttpResponse resp3 = client.execute(http3);
            InputStream instream3 = resp3.getEntity().getContent();
            String rpta3 = IOUtils.toString(instream3, "UTF-8");

            rpta3 = rpta3.substring(rpta3.indexOf("__VIEWSTATE") + 12, rpta3.length());
            rpta3 = rpta3.substring(rpta3.indexOf("value") + 7, rpta3.length());
            __VIEWSTATE = rpta3.substring(0, rpta3.indexOf("\""));

            rpta3 = rpta3.substring(rpta3.indexOf("__EVENTVALIDATION") + 18, rpta3.length());
            rpta3 = rpta3.substring(rpta3.indexOf("value") + 7, rpta3.length());
            __EVENTVALIDATION = rpta3.substring(0, rpta3.indexOf("\""));
        } catch (IOException | IllegalStateException ep) {
            System.out.println("[ProcesadorPlaca:constructor] Error:" + ep.getMessage());
        }

        System.out.println("Creando archivo log... efestos_thread_" + this.ID + ".log");
    }

    public List<Placa> getPlacas() {
        return placas;
    }

    public void setPlacas(List<Placa> placas) {
        this.placas = placas;
    }

    public List parametros3(String placa, String elemento, String v, String v2) {
        NameValuePair p_2 = new BasicNameValuePair("__EVENTTARGET", "ctl00$cplPrincipal$grdAdministrados$ctl02$lnkCodigo");
        NameValuePair p_3 = new BasicNameValuePair("__EVENTARGUMENT", "");
        NameValuePair p_4 = new BasicNameValuePair("__VIEWSTATE", v);
//        NameValuePair p_1 = new BasicNameValuePair("__LASTFOCUS", "");
        NameValuePair p_5 = new BasicNameValuePair("__EVENTVALIDATION", v2);
        NameValuePair p_6 = new BasicNameValuePair("ctl00$cplPrincipal$hidPaterno", "");
        NameValuePair p_7 = new BasicNameValuePair("ctl00$cplPrincipal$hidMaterno:", "");
        NameValuePair p_8 = new BasicNameValuePair("ctl00$cplPrincipal$hidNombre:", "");
        NameValuePair p_9 = new BasicNameValuePair("ctl00$cplPrincipal$hidRazon:", "");
        NameValuePair p_10 = new BasicNameValuePair("ctl00$cplPrincipal$hidCodVeh", elemento + "*");
        NameValuePair p_11 = new BasicNameValuePair("ctl00$cplPrincipal$hidPlaca", placa);

        List loginNvps = new ArrayList();
//        loginNvps.add(p_1);
        loginNvps.add(p_2);
        loginNvps.add(p_3);
        loginNvps.add(p_4);
        loginNvps.add(p_5);
        loginNvps.add(p_6);
        loginNvps.add(p_7);
        loginNvps.add(p_8);
        loginNvps.add(p_9);
        loginNvps.add(p_10);
        loginNvps.add(p_11);
        return loginNvps;
    }

    public List parametros3_1(String v, String v2, String extra2) {
        NameValuePair p_2 = new BasicNameValuePair("__EVENTTARGET", "");
        NameValuePair p_3 = new BasicNameValuePair("__EVENTARGUMENT", "");
        NameValuePair p_1 = new BasicNameValuePair("__LASTFOCUS", "");
        NameValuePair p_4 = new BasicNameValuePair("__VIEWSTATE", v);
        NameValuePair p_5 = new BasicNameValuePair("__EVENTVALIDATION", v2);
        NameValuePair p_6 = new BasicNameValuePair("ctl00$cplPrincipal$ddlAnio", "0");
        NameValuePair p_7 = new BasicNameValuePair("ctl00$cplPrincipal$ddlEstado:", "2");
        NameValuePair p_8 = new BasicNameValuePair("ctl00$cplPrincipal$btnBuscar:", "Actualizar");
        NameValuePair p_9 = new BasicNameValuePair("ctl00$cplPrincipal$ddlReferencia:", "");
        NameValuePair p_10 = new BasicNameValuePair("ctl00$cplPrincipal$hidUrlPrint", extra2);

        List loginNvps = new ArrayList();
        loginNvps.add(p_1);
        loginNvps.add(p_2);
        loginNvps.add(p_3);
        loginNvps.add(p_4);
        loginNvps.add(p_5);
        loginNvps.add(p_6);
        loginNvps.add(p_7);
        loginNvps.add(p_8);
        loginNvps.add(p_9);
        loginNvps.add(p_10);
        return loginNvps;
    }

    public List parametros(String placa, String v, String v2) {
        NameValuePair p_2 = new BasicNameValuePair("__EVENTTARGET", "");
        NameValuePair p_3 = new BasicNameValuePair("__EVENTARGUMENT", "");
        NameValuePair p_4 = new BasicNameValuePair("__VIEWSTATE", v);
        NameValuePair p_1 = new BasicNameValuePair("__LASTFOCUS", "");
        NameValuePair p_5 = new BasicNameValuePair("__EVENTVALIDATION", v2);
        NameValuePair p_6 = new BasicNameValuePair("ctl00$cplPrincipal$txtPlaca", placa);
        NameValuePair p_7 = new BasicNameValuePair("ctl00$cplPrincipal$btnBuscar", "Buscar");
        NameValuePair p_8 = new BasicNameValuePair("ctl00$cplPrincipal$txtPapeleta", "");

        List loginNvps = new ArrayList();
        loginNvps.add(p_1);
        loginNvps.add(p_2);
        loginNvps.add(p_3);
        loginNvps.add(p_4);
        loginNvps.add(p_5);
        loginNvps.add(p_6);
        loginNvps.add(p_7);
        loginNvps.add(p_8);
        return loginNvps;
    }

    public List parametros_1(String placa, String v, String v2, String extra) {
        NameValuePair p_2 = new BasicNameValuePair("__EVENTTARGET", "ctl00$cplPrincipal$grdEstadoCuenta");
        NameValuePair p_3 = new BasicNameValuePair("__EVENTARGUMENT", extra);
        NameValuePair p_4 = new BasicNameValuePair("__VIEWSTATE", v);
        NameValuePair p_1 = new BasicNameValuePair("__LASTFOCUS", "");
        NameValuePair p_5 = new BasicNameValuePair("__EVENTVALIDATION", v2);
        NameValuePair p_6 = new BasicNameValuePair("ctl00$cplPrincipal$txtPlaca", placa);
//        NameValuePair p_7 = new BasicNameValuePair("ctl00$cplPrincipal$btnBuscar", "Buscar");
        NameValuePair p_8 = new BasicNameValuePair("ctl00$cplPrincipal$txtPapeleta", "");

        List loginNvps = new ArrayList();
        loginNvps.add(p_1);
        loginNvps.add(p_2);
        loginNvps.add(p_3);
        loginNvps.add(p_4);
        loginNvps.add(p_5);
        loginNvps.add(p_6);
//        loginNvps.add(p_7);
        loginNvps.add(p_8);
        return loginNvps;
    }

    public List parametros2(String placa, String v, String v2) {
        NameValuePair p_2 = new BasicNameValuePair("__EVENTTARGET", "");
        NameValuePair p_3 = new BasicNameValuePair("__EVENTARGUMENT", "");
        NameValuePair p_4 = new BasicNameValuePair("__VIEWSTATE", v);
        NameValuePair p_1 = new BasicNameValuePair("__LASTFOCUS", "");
        NameValuePair p_5 = new BasicNameValuePair("__EVENTVALIDATION", v2);
        NameValuePair p_6 = new BasicNameValuePair("ctl00$cplPrincipal$ddlTipoDocu", "2");
        NameValuePair p_7 = new BasicNameValuePair("ctl00$cplPrincipal$txtDocumento", "");
        NameValuePair p_8 = new BasicNameValuePair("ctl00$cplPrincipal$txtPaterno", "");
        NameValuePair p_9 = new BasicNameValuePair("ctl00$cplPrincipal$txtMaterno", "");
        NameValuePair p_10 = new BasicNameValuePair("ctl00$cplPrincipal$txtNombre", "");
        NameValuePair p_11 = new BasicNameValuePair("ctl00$cplPrincipal$txtRazon", "");
        NameValuePair p_12 = new BasicNameValuePair("ctl00$cplPrincipal$txtPlaca", placa);
        NameValuePair p_13 = new BasicNameValuePair("ctl00$cplPrincipal$btnBuscaPlaca", "Buscar");
        NameValuePair p_14 = new BasicNameValuePair("ctl00$cplPrincipal$hidPaterno", "");
        NameValuePair p_15 = new BasicNameValuePair("ctl00$cplPrincipal$hidMaterno", "");
        NameValuePair p_16 = new BasicNameValuePair("ctl00$cplPrincipal$hidNombre", "");
        NameValuePair p_17 = new BasicNameValuePair("ctl00$cplPrincipal$hidRazon", "");
        NameValuePair p_18 = new BasicNameValuePair("ctl00$cplPrincipal$hidCodVeh", "");
        NameValuePair p_19 = new BasicNameValuePair("ctl00$cplPrincipal$hidPlaca", "");

        List loginNvps = new ArrayList();
        loginNvps.add(p_1);
        loginNvps.add(p_2);
        loginNvps.add(p_3);
        loginNvps.add(p_4);
        loginNvps.add(p_5);
        loginNvps.add(p_6);
        loginNvps.add(p_7);
        loginNvps.add(p_8);
        loginNvps.add(p_9);
        loginNvps.add(p_10);
        loginNvps.add(p_11);
        loginNvps.add(p_12);
        loginNvps.add(p_13);
        loginNvps.add(p_14);
        loginNvps.add(p_15);
        loginNvps.add(p_16);
        loginNvps.add(p_17);
        loginNvps.add(p_18);
        loginNvps.add(p_19);
        return loginNvps;
    }

    public List<Papeletas> procesar_papeletas(String detalle) {
        String detalle_ori = detalle;
        List<Papeletas> pap = new ArrayList<>();
        boolean s = true;
        String xfr = "";
        while (s) {
            try {
                if (detalle.indexOf("<tr") > -1) {
                    String px = detalle.substring(detalle.indexOf(">") + 1, detalle.indexOf("</tr>")).trim();
                    detalle = detalle.substring(detalle.indexOf("</tr>") + 5, detalle.length()).trim();
                    if (px.indexOf("<td") > -1) {
                        boolean tds = true;
                        while (tds) {
                            if (px.indexOf("<td") > -1) {
                                String ctd = px.substring(px.indexOf(">") + 1, px.indexOf("</td>"));

                                try {
                                    ctd = ctd.substring(ctd.indexOf(">") + 1, ctd.indexOf("</span>") > -1 ? ctd.indexOf("</span>") : ctd.length()).trim();
                                } catch (Exception ep) {
                                    ctd = "";
                                }
                                px = px.substring(px.indexOf("</td>") + 5, px.length());
                                xfr = xfr + ctd + ",";
                            } else {
                                tds = false;
                            }
                        }
                    }
                } else {
                    s = false;
                }
                Papeletas papeleta = null;
                String[] dat = xfr.split(",");
                if (dat.length > 5 && dat.length < 20) {
                    papeleta = new Papeletas();
//                    papeleta.setPlaca(dat[2]);
                    papeleta.setPlaca((dat[2] + "").contentEquals("") ? "" : dat[2]);
                    papeleta.setConcepto(dat[3]);
                    papeleta.setCodigopago(dat[4]);
                    papeleta.setFecinf_fecemi(dat[5]);
                    papeleta.setImporte(Util.convertir_double(dat[6]));
                    papeleta.setGast_cost(Util.convertir_double(dat[7]));
                    papeleta.setDescuento(Util.convertir_double(dat[8]));
                    try {
                        papeleta.setTotal_pagar(Double.parseDouble(dat[10]));
                    } catch (Exception ep) {
                        papeleta.setTotal_pagar(0);
                    }
                    papeleta.setEstado(dat[11]);
                    try {
                        papeleta.setFalta(dat[12]);
                    } catch (Exception ep) {
                        papeleta.setFalta("");
//                        System.out.println("ep:"+ep.getMessage());
//                        ep.printStackTrace();
                    }
                    try {
                        papeleta.setLicencia(dat[13]);
                    } catch (Exception r3) {
                        papeleta.setLicencia("");
                    }
                    pap.add(papeleta);
                }
                xfr = "";
            } catch (Exception ep) {
                System.out.println(">>>>Error: " + ep.getMessage() + "\n*************************************************************************\n" + detalle_ori + "\n****************************************************************************");
                ep.printStackTrace();
                s = false;
            }
        }
        return pap;
    }

    public List<Multas> procesar_impuestos(String detalle) {
        String detalle_ori = detalle;
        List<Multas> mul = new ArrayList<>();
        boolean s = true;
        String xfr = "";
        while (s) {
            try {
                if (detalle.indexOf("<tr") > -1) {
                    String px = detalle.substring(detalle.indexOf(">") + 1, detalle.indexOf("</tr>")).trim();
                    detalle = detalle.substring(detalle.indexOf("</tr>") + 5, detalle.length()).trim();
                    if (px.indexOf("<td") > -1) {
                        boolean tds = true;
                        while (tds) {
                            if (px.indexOf("<td") > -1) {
                                String ctd = px.substring(px.indexOf(">") + 1, px.indexOf("</td>"));

                                try {
                                    ctd = ctd.substring(ctd.indexOf(">") + 1, ctd.indexOf("</span>") > -1 ? ctd.indexOf("</span>") : ctd.length()).trim();
                                } catch (Exception ep) {
                                    ctd = "";
                                }
                                px = px.substring(px.indexOf("</td>") + 5, px.length());
//                                System.out.println("xfr:"+ctd);
                                xfr = xfr + ctd + ",";
                            } else {
                                tds = false;
                            }
                        }
                    }
                } else {
                    s = false;
                }
                Multas multa = null;
                String[] dat = xfr.split(",");
//                System.out.println(">[" + xfr + "]>" + dat.length);
                if (dat.length > 5 && dat.length < 20) {
                    //Elizabeth.convertir_double(dat[6])
                    multa = new Multas();
                    multa.setAnhio(dat[1]);
                    multa.setCuota(dat[2]);
                    multa.setCodigo_pago(dat[3]);
                    multa.setDeuda_total(Util.convertir_double(dat[4]));
                    multa.setPagado(Util.convertir_double(dat[5]));
                    multa.setVencimiento(dat[6]);
                    multa.setEstado(dat[7]);
                    multa.setEstado_documento_deuda(dat[8]);
                    multa.setEstado_documento_notificado(dat[9]);
                    multa.setFecha_notificacion(dat[10]);
                    multa.setEstado_expedi(dat[11]);
                    multa.setReferencia(dat[12]);
                    mul.add(multa);
                }
                xfr = "";
            } catch (Exception ep) {
                System.out.println(">>>>Error: " + ep.getMessage() + "\n*************************************************************************\n" + detalle_ori + "\n****************************************************************************");
                ep.printStackTrace();
                s = false;
            }
        }
        return mul;
    }

    public List<Multas> procesar_impuestos_cancelados(String detalle) {
        String detalle_ori = detalle;
        List<Multas> mul = new ArrayList<>();
        boolean s = true;
        String xfr = "";
        while (s) {
            try {
                if (detalle.indexOf("<tr") > -1) {
                    String px = detalle.substring(detalle.indexOf(">") + 1, detalle.indexOf("</tr>")).trim();
                    detalle = detalle.substring(detalle.indexOf("</tr>") + 5, detalle.length()).trim();
                    if (px.indexOf("<td") > -1) {
                        boolean tds = true;
                        while (tds) {
                            if (px.indexOf("<td") > -1) {
                                String ctd = px.substring(px.indexOf(">") + 1, px.indexOf("</td>"));

                                try {
                                    ctd = ctd.substring(ctd.indexOf(">") + 1, ctd.indexOf("</span>") > -1 ? ctd.indexOf("</span>") : ctd.length()).trim();
                                } catch (Exception ep) {
                                    ctd = "";
                                }
                                px = px.substring(px.indexOf("</td>") + 5, px.length());
//                                System.out.println("xfr:"+ctd);
                                xfr = xfr + ctd + ",";
                            } else {
                                tds = false;
                            }
                        }
                    }
                } else {
                    s = false;
                }
                Multas multa = null;
                String[] dat = xfr.split(",");
                System.out.println(">[" + xfr + "]>" + dat.length);
                if (dat.length > 5 && dat.length < 20) {
                    //Elizabeth.convertir_double(dat[6])
                    multa = new Multas();
                    multa.setAnhio(dat[1]);
                    multa.setCuota(dat[2]);
                    multa.setCodigo_pago(dat[3]);
                    multa.setDeuda_total(Util.convertir_double(dat[4]));
                    multa.setPagado(Util.convertir_double(dat[5]));
                    multa.setVencimiento(dat[6]);
                    multa.setEstado(dat[7]);
                    multa.setEstado_documento_deuda(dat[8]);
                    multa.setEstado_documento_notificado(dat[9]);
                    multa.setFecha_notificacion(dat[10]);
                    multa.setEstado_expedi(dat[11]);
                    multa.setReferencia(dat[12]);
                    mul.add(multa);
                }
                xfr = "";
            } catch (Exception ep) {
                System.out.println(">>>>Error: " + ep.getMessage() + "\n*************************************************************************\n" + detalle_ori + "\n****************************************************************************");
                ep.printStackTrace();
                s = false;
            }
        }
        return mul;
    }

    @Override
    public void run() {
        for (int i = 0; i < placas.size(); i++) {
            Placa placa = placas.get(i);
            placa.setTHREAD_ID("" + ID);
            placa.setSUBTHREAD_ID("" + (i + 1) + " de " + placas.size());
            String nurl = rk;
            String __VIEWSTATE2 = __VIEWSTATE;
            String __EVENTVALIDATION2 = __EVENTVALIDATION;
            try {
                HttpPost http4 = new HttpPost(nurl);
                http4.setEntity(new UrlEncodedFormEntity(parametros(placa.getCo_plaveh(), __VIEWSTATE2, __EVENTVALIDATION2), "UTF-8"));
                org.apache.http.HttpResponse resp4 = client.execute(http4);
                String rpta4 = IOUtils.toString(resp4.getEntity().getContent(), "UTF-8");
                try {
                    rpta4 = rpta4.substring(rpta4.indexOf("__VIEWSTATE") + 12, rpta4.length());
                    rpta4 = rpta4.substring(rpta4.indexOf("value") + 7, rpta4.length());
                    __VIEWSTATE2 = rpta4.substring(0, rpta4.indexOf("\""));
                    rpta4 = rpta4.substring(rpta4.indexOf("__EVENTVALIDATION") + 18, rpta4.length());
                    rpta4 = rpta4.substring(rpta4.indexOf("value") + 7, rpta4.length());
                    __EVENTVALIDATION2 = rpta4.substring(0, rpta4.indexOf("\""));
                    //------------------------------------------------------------------------------------------------------------------------------------------------------
                    rpta4 = rpta4.substring(rpta4.indexOf("ctl00_cplPrincipal_grdEstadoCuenta"), rpta4.length());
                    rpta4 = rpta4.substring(rpta4.indexOf(">") + 1, rpta4.length());
                    String detalle = rpta4.substring(0, rpta4.indexOf("</table>")).trim();
                    placa.setLs_papeletas(procesar_papeletas(detalle));
                    rpta4 = rpta4.substring(rpta4.indexOf("Monto total a pagar"), rpta4.length());
                    rpta4 = rpta4.substring(rpta4.indexOf("<b>") + 3, rpta4.length());
                    rpta4 = rpta4.substring(0, rpta4.indexOf("</b>"));
                    rpta4 = rpta4.replaceAll("S/.", "");
                    rpta4 = rpta4.replaceAll(",", "").trim();
                    placa.setMo_papveh(Double.parseDouble(rpta4));
                } catch (Exception ep2) {
                    placa.setMo_papveh(Double.parseDouble("0"));
                }
//                WRITE(System.currentTimeMillis() + ":" + (i + 1) + " de " + placas.size() + "\ten proceso:" + placa.getCo_plaveh() + "..1");

                if (placa.getLs_papeletas() != null & placa.getLs_papeletas().size() == 10) {
                    HttpPost http4_1 = new HttpPost(nurl);
                    http4_1.setEntity(new UrlEncodedFormEntity(parametros_1(placa.getCo_plaveh(), __VIEWSTATE2, __EVENTVALIDATION2, "Page$2"), "UTF-8"));
                    org.apache.http.HttpResponse resp4_1 = client.execute(http4_1);
                    String rpta4_1 = IOUtils.toString(resp4_1.getEntity().getContent(), "UTF-8");
                    try {

                        rpta4_1 = rpta4_1.substring(rpta4_1.indexOf("ctl00_cplPrincipal_grdEstadoCuenta"), rpta4_1.length());
                        rpta4_1 = rpta4_1.substring(rpta4_1.indexOf(">") + 1, rpta4_1.length());
                        String detalle = rpta4_1.substring(0, rpta4_1.indexOf("</table>")).trim();
                        placa.setLs_papeletas2(procesar_papeletas(detalle));
                    } catch (Exception ep2) {
                        placa.setMo_papveh(Double.parseDouble("0"));
                    }
                }
                if (placa.getLs_papeletas() != null & placa.getLs_papeletas().size() == 20) {
                    HttpPost http4_1 = new HttpPost(nurl);
                    http4_1.setEntity(new UrlEncodedFormEntity(parametros_1(placa.getCo_plaveh(), __VIEWSTATE2, __EVENTVALIDATION2, "Page$3"), "UTF-8"));
                    org.apache.http.HttpResponse resp4_1 = client.execute(http4_1);
                    String rpta4_1 = IOUtils.toString(resp4_1.getEntity().getContent(), "UTF-8");
                    try {

                        rpta4_1 = rpta4_1.substring(rpta4_1.indexOf("ctl00_cplPrincipal_grdEstadoCuenta"), rpta4_1.length());
                        rpta4_1 = rpta4_1.substring(rpta4_1.indexOf(">") + 1, rpta4_1.length());
                        String detalle = rpta4_1.substring(0, rpta4_1.indexOf("</table>")).trim();
                        placa.setLs_papeletas2(procesar_papeletas(detalle));
                    } catch (Exception ep2) {
                        placa.setMo_papveh(Double.parseDouble("0"));
                    }
                }
                if (placa.getLs_papeletas() != null & placa.getLs_papeletas().size() == 30) {
                    HttpPost http4_1 = new HttpPost(nurl);
                    http4_1.setEntity(new UrlEncodedFormEntity(parametros_1(placa.getCo_plaveh(), __VIEWSTATE2, __EVENTVALIDATION2, "Page$4"), "UTF-8"));
                    org.apache.http.HttpResponse resp4_1 = client.execute(http4_1);
                    String rpta4_1 = IOUtils.toString(resp4_1.getEntity().getContent(), "UTF-8");
                    try {

                        rpta4_1 = rpta4_1.substring(rpta4_1.indexOf("ctl00_cplPrincipal_grdEstadoCuenta"), rpta4_1.length());
                        rpta4_1 = rpta4_1.substring(rpta4_1.indexOf(">") + 1, rpta4_1.length());
                        String detalle = rpta4_1.substring(0, rpta4_1.indexOf("</table>")).trim();
                        placa.setLs_papeletas2(procesar_papeletas(detalle));
                    } catch (Exception ep2) {
                        placa.setMo_papveh(Double.parseDouble("0"));
                    }
                }
                if (placa.getLs_papeletas() != null & placa.getLs_papeletas().size() == 40) {
                    HttpPost http4_1 = new HttpPost(nurl);
                    http4_1.setEntity(new UrlEncodedFormEntity(parametros_1(placa.getCo_plaveh(), __VIEWSTATE2, __EVENTVALIDATION2, "Page$5"), "UTF-8"));
                    org.apache.http.HttpResponse resp4_1 = client.execute(http4_1);
                    String rpta4_1 = IOUtils.toString(resp4_1.getEntity().getContent(), "UTF-8");
                    try {

                        rpta4_1 = rpta4_1.substring(rpta4_1.indexOf("ctl00_cplPrincipal_grdEstadoCuenta"), rpta4_1.length());
                        rpta4_1 = rpta4_1.substring(rpta4_1.indexOf(">") + 1, rpta4_1.length());
                        String detalle = rpta4_1.substring(0, rpta4_1.indexOf("</table>")).trim();
                        placa.setLs_papeletas2(procesar_papeletas(detalle));
                    } catch (Exception ep2) {
                        placa.setMo_papveh(Double.parseDouble("0"));
                    }
                }
//                WRITE(System.currentTimeMillis() + ":" + (i + 1) + " de " + placas.size() + "\ten proceso:" + placa.getCo_plaveh() + "..2");
                /*
                 Codigo extra
                 */
                //***************************************************************************
                try {
                    nurl = nurl.replace("papeletas", "BusquedaTributario") + "&tri=V";
                    HttpGet http5 = new HttpGet(nurl);
                    HttpResponse resp5 = client.execute(http5);
                    String rpta5 = IOUtils.toString(resp5.getEntity().getContent(), "UTF-8");
                    rpta5 = rpta5.substring(rpta5.indexOf("__VIEWSTATE") + 12, rpta5.length());
                    rpta5 = rpta5.substring(rpta5.indexOf("value") + 7, rpta5.length());
                    __VIEWSTATE2 = rpta5.substring(0, rpta5.indexOf("\""));
                    rpta5 = rpta5.substring(rpta5.indexOf("__EVENTVALIDATION") + 18, rpta5.length());
                    rpta5 = rpta5.substring(rpta5.indexOf("value") + 7, rpta5.length());
                    __EVENTVALIDATION2 = rpta5.substring(0, rpta5.indexOf("\""));

                    HttpPost http6 = new HttpPost(nurl);
                    http6.setEntity(new UrlEncodedFormEntity(parametros2(placa.getCo_plaveh(), __VIEWSTATE2, __EVENTVALIDATION2), "UTF-8"));
                    HttpResponse resp6 = client.execute(http6);
                    String rpta6 = IOUtils.toString(resp6.getEntity().getContent(), "UTF-8");
                    rpta6 = rpta6.substring(rpta6.indexOf("__VIEWSTATE") + 12, rpta6.length());
                    rpta6 = rpta6.substring(rpta6.indexOf("value") + 7, rpta6.length());
                    __VIEWSTATE2 = rpta6.substring(0, rpta6.indexOf("\""));
                    rpta6 = rpta6.substring(rpta6.indexOf("__EVENTVALIDATION") + 18, rpta6.length());
                    rpta6 = rpta6.substring(rpta6.indexOf("value") + 7, rpta6.length());
                    __EVENTVALIDATION2 = rpta6.substring(0, rpta6.indexOf("\""));

                    rpta6 = rpta6.substring(rpta6.indexOf("ctl00_cplPrincipal_hidCodVeh") + 1, rpta6.length());
                    rpta6 = rpta6.substring(rpta6.indexOf("value") + 7, rpta6.length());
                    rpta6 = rpta6.substring(0, rpta6.indexOf("*"));
//                    System.out.println("nurl-http7:" + nurl);
                    HttpPost http7 = new HttpPost(nurl);
                    http7.setEntity(new UrlEncodedFormEntity(parametros3(placa.getCo_plaveh(), rpta6, __VIEWSTATE2, __EVENTVALIDATION2), "UTF-8"));
                    HttpResponse resp7 = client.execute(http7);
                    String rpta7 = IOUtils.toString(resp7.getEntity().getContent(), "UTF-8");

                    rpta7 = rpta7.substring(rpta7.indexOf("TributosRef.aspx?"), rpta7.length());
                    String rmk = rpta7.substring(0, rpta7.indexOf("\""));
                    rmk = rmk.replaceAll("amp;", "");
//                    System.out.println("rmk:" + rmk);

                    rpta7 = rpta7.substring(rpta7.indexOf("__VIEWSTATE") + 12, rpta7.length());
                    rpta7 = rpta7.substring(rpta7.indexOf("value") + 7, rpta7.length());
                    __VIEWSTATE2 = rpta7.substring(0, rpta7.indexOf("\""));
                    rpta7 = rpta7.substring(rpta7.indexOf("__EVENTVALIDATION") + 18, rpta7.length());
                    rpta7 = rpta7.substring(rpta7.indexOf("value") + 7, rpta7.length());
                    __EVENTVALIDATION2 = rpta7.substring(0, rpta7.indexOf("\""));
//------------------------------------------------------------------
//                    String extra2 = rpta7.substring(rpta7.indexOf("ctl00$cplPrincipal$hidUrlPrint"), rpta7.length());
//                    extra2 = extra2.substring(extra2.indexOf("value") + 7, extra2.indexOf("/")).trim();
//                    extra2 = extra2.substring(0, extra2.length() - 1).trim();
//                    extra2 = extra2.replaceAll("amp;", "");
//                    System.out.println("extra2:" + extra2);
//------------------------------------------------------------------

                    rpta7 = rpta7.substring(rpta7.indexOf("ctl00_cplPrincipal_grdEstadoCuenta"), rpta7.length());
                    rpta7 = rpta7.substring(rpta7.indexOf(">") + 1, rpta7.length());
                    String detalle = rpta7.substring(0, rpta7.indexOf("</table>")).trim();
                    placa.setLs_multas(procesar_impuestos(detalle));
//                    System.out.println("rpta7:" + detalle);

                    rpta7 = rpta7.substring(rpta7.indexOf("Total a pagar"), rpta7.length());
                    rpta7 = rpta7.substring(rpta7.indexOf("<b>") + 3, rpta7.length());
                    rpta7 = rpta7.substring(0, rpta7.indexOf("</b>"));
                    rpta7 = rpta7.replaceAll("S/.", "");
                    rpta7 = rpta7.replaceAll(",", "").trim();

//                    System.out.println("=============>" + nurl);
//                    nurl = nurl.replace("BusquedaTributario", "TributosRef");
//                    System.out.println("https://www.sat.gob.pe/homesite2007/modulos/" + rmk);
//                    HttpPost http8 = new HttpPost("https://www.sat.gob.pe/homesite2007/modulos/" + rmk);
//                    http8.setEntity(new UrlEncodedFormEntity(parametros3_1( __VIEWSTATE2, __EVENTVALIDATION2, extra2), "UTF-8"));
//                    HttpResponse resp8 = client.execute(http8);
//                    String rpta8 = IOUtils.toString(resp8.getEntity().getContent(), "UTF-8");
//                    rpta8 = rpta8.substring(rpta8.indexOf("ctl00_cplPrincipal_grdEstadoCuenta"), rpta8.length());
//                    rpta8 = rpta8.substring(rpta8.indexOf(">") + 1, rpta8.length());
//                    String detalle2 = rpta8.substring(0, rpta8.indexOf("</table>")).trim();
//                    List<Multas> ls_multa = procesar_impuestos(detalle2);
//                    for (Multas multa : ls_multa) {
//                        System.out.println("multa:" + multa.toString());
//                    }
                    placa.setMo_impveh(Double.parseDouble(rpta7));
                } catch (Exception ep2) {
                    placa.setMo_impveh(Double.parseDouble("0"));
                }

                ColaProcesamiento.nuevoProceso(placa);
//                WRITE(System.currentTimeMillis() + ":" + (i + 1) + " de " + placas.size() + "\tReady:" + placa.toString());
            } catch (Exception ep) {
                placa.setMo_papveh(Double.parseDouble("0"));
                ColaProcesamiento.nuevoProceso(placa);
//                WRITE(System.currentTimeMillis() + ":" + (i + 1) + " de " + placas.size() + "\tReady:" + placa.toString());
            }
            if (i == this.placas.size() - 1) {
//                try {
//                    archivo.close();
//                } catch (Exception ep) {
//                }
            }
        }

    }

}
